package main;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import twitter.entity.Tweet;
import twitter.functionalities.FollowUnfollow;
import twitter.functionalities.MessageSender;
import twitter.functionalities.StatComputer;
import twitter.tool.Database;
import twitter.tool.Parser;
import twitter4j.Paging;
import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.User;

public class TwitterMain {

	public static void main(String[] args) throws TwitterException,
			ClassNotFoundException, SQLException, IOException {

		Class.forName("com.mysql.jdbc.Driver");
		Connection conn = DriverManager
				.getConnection("jdbc:mysql://localhost/analizer?user=root&password=");
		Database d = new Database(conn);

		// d.createTableTweet();
		// d.createTableStat();

		Twitter twitter = TwitterFactory.getSingleton();
		List<Status> statuses = new ArrayList<Status>();
		User user = twitter.getUserTimeline().get(0).getUser();

		int page = user.getStatusesCount() / 20 + 1;
		for (int i = 1; i <= page; i++) {
			Paging paging = new Paging(i);
			statuses.addAll(twitter.getUserTimeline(paging));
		}

		List<Tweet> tweets = new ArrayList<Tweet>();
		Parser parser = new Parser();
		tweets = parser.parseTweets(statuses);
		d.updateTweet(tweets);

		StatComputer sC = new StatComputer(d);
		d.insertStat(sC.computeStat(user.getFollowersCount(),
				user.getFriendsCount()));
		// sC.printDailyNumberOfP(tweets, conn);

		// d.createTableSimilarAccount();
		String[] twitterNames = { "rockonitalia" };
		String[] facebookNames = { "RockON.it" };
		d.insertSimilarAccount(parser.parseSimilarAccount(twitterNames,
				facebookNames));

		FollowUnfollow fu = new FollowUnfollow(twitter);
		fu.setUp(user.getScreenName());
		// fu.unfollow();
		// fu.follow("_KeepOn");
		// fu.follow("Bastilleblog");

		// MessageSender ms = new MessageSender(twitter);
		// ms.sendMessages(user.getScreenName(), null);

		// tweets = d.getAllTweet();
		// tweets = d.searchTag(tweets,"Arctic");
		// int i = 0;
		// for (Tweet tweet: tweets){
		// System.out.println(i);
		// System.out.println(tweet.getDate());
		// System.out.println(tweet.getText());
		// System.out.println(tweet.getURL());
		// System.out.println(tweet.getContent());
		// for(int j = 0; j< tweet.getHashtags().size(); j++){
		// System.out.println(tweet.getHashtags().get(j));
		// }
		// i++;
		// }

		conn.close();

	}
}
